using Digitalmes.Application.Mes.Equipments.Inspections.Commands;
using Digitalmes.Application.Mes.Equipments.Inspections;
using Digitalmes.Application.Mes.Equipments.Inspections.Queries;

namespace Digitalmes.WebApi.Endpoints.Mes.Equipments;

/// <summary>
/// 点检项终结点。
/// </summary>
public sealed class InspectionItemEndpoint : EndpointGroupBase
{
    public override void Map(WebApplication app)
    {
        app.MapGroup(this, "mes/equipment/inspection", "item")
            .RequireAuthorization()
            .MapGet(GetManualInspectionItemListAsync, "manual/list")
            .MapGet(GetAutoInspectionItemListAsync, "auto/list")
            .MapPost(CreateManualInspectionItemAsync, "manual")
            .MapPost(CreateAutoInspectionItemAsync, "auto")
            .MapPut(UpdateManualInspectionItemAsync, "manual/{id}")
            .MapPut(UpdateAutoInspectionItemAsync, "auto/{id}")
            .MapDelete(DeleteManualInspectionItemAsync, "manual/{id}")
            .MapDelete(DeleteAutoInspectionItemAsync, "auto/{id}");
    }

    [EndpointDescription("查询手动点检项")]
    public async Task<IApiResult> GetManualInspectionItemListAsync(ISender sender, [AsParameters] GetManualInspectionItemListQuery query)
    {
        var result = await sender.Send(query);
        return result.ToApiResult();
    }

    [EndpointDescription("创建手动点检项")]
    public async Task<IApiResult> CreateManualInspectionItemAsync(ISender sender, EquipManualInspectionItemDto dto)
    {
        var result = await sender.Send(new CreateManualInspectionItemCommand(dto));
        return result.ToApiResult();
    }

    [EndpointDescription("更新手动点检项")]
    public async Task<IApiResult> UpdateManualInspectionItemAsync(ISender sender, long id, EquipManualInspectionItemDto dto)
    {
        var result = await sender.Send(new UpdateManualInspectionItemCommand(id, dto));
        return result.ToApiResult();
    }

    [EndpointDescription("删除手动点检项")]
    public async Task<IApiResult> DeleteManualInspectionItemAsync(ISender sender, long id)
    {
        var result = await sender.Send(new DeleteManualInspectionItemCommand(id));
        return result.ToApiResult();
    }

    [EndpointDescription("查询自动点检项")]
    public async Task<IApiResult> GetAutoInspectionItemListAsync(ISender sender, [AsParameters] GetAutoInspectionItemListQuery query)
    {
        var result = await sender.Send(query);
        return result.ToApiResult();
    }

    [EndpointDescription("创建自动点检项")]
    public async Task<IApiResult> CreateAutoInspectionItemAsync(ISender sender, EquipAutoInspectionItemDto dto)
    {
        var result = await sender.Send(new CreateAutoInspectionItemCommand(dto));
        return result.ToApiResult();
    }

    [EndpointDescription("更新自动点检项")]
    public async Task<IApiResult> UpdateAutoInspectionItemAsync(ISender sender, long id, EquipAutoInspectionItemDto dto)
    {
        var result = await sender.Send(new UpdateAutoInspectionItemCommand(id, dto));
        return result.ToApiResult();
    }

    [EndpointDescription("删除自动点检项")]
    public async Task<IApiResult> DeleteAutoInspectionItemAsync(ISender sender, long id)
    {
        var result = await sender.Send(new DeleteAutoInspectionItemCommand(id));
        return result.ToApiResult();
    }
}
